package com.cloud.sso.oauth.handler;

import com.alibaba.fastjson.JSON;
import com.cloud.sso.common.config.SSOProperty;
import com.cloud.sso.common.util.JsonContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;

@Configuration
public class AppLogoutSuccessHandler implements LogoutSuccessHandler {

    @Autowired
    private SSOProperty ssoProperty;

    @Override
    public void onLogoutSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        JsonContent data = JsonContent.success().setData(ssoProperty.getLogoutUrl());
        String jsonData = new String(JSON.toJSONString(data).getBytes(), "UTF-8");
        Writer writer = httpServletResponse.getWriter();

        writer.write(jsonData);
        writer.flush();
        writer.close();
    }
}
